﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using MySqlQueryToolkit;
using MySqlQueryToolkit.DataAccess;

namespace MySqlQueryToolkitTests
{
    [TestClass]
    public class ProfileQueryRepositoryTests
    {
        private IDataAccess _dataAccess;
        private IUnsafeDataAccess _unsafeDataAccess;

        [TestInitialize]
        public void TestInitialize()
        {
            const string connectionString = "Server=localhost;Uid=root;Pwd=root;port=3306;";
            _dataAccess = new MySqlDataAccess(connectionString);
            _unsafeDataAccess = new UnsafeMySqlDataAccess(connectionString);
            _dataAccess.CommandText =
                @"CREATE TABLE IF NOT EXISTS `test`.`tblTest` (
                `Id` INT NOT NULL AUTO_INCREMENT ,
                `Name` VARCHAR(45) NULL ,
                PRIMARY KEY (`Id`) );
                INSERT INTO test.tblTest (Name) VALUES ('Bob'), ('James');
            ";
            _dataAccess.ExecuteNonQuery();
        }

        [TestCleanup]
        public void TestCleanup()
        {
            _dataAccess.CommandText = "DROP TABLE IF EXISTS `test`.`tblTest`;";
            _dataAccess.ExecuteNonQuery();
        }

        [TestMethod]
        [TestCategory("Integration")]
        public void GetProfileQueryExpectCorrectObject()
        {
            var target = new ProfileQueryRepository(_unsafeDataAccess);

            var result = target.GetEntitiesFromDatabase("SELECT * FROM test.tblTest;");

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.HandlerCommit);
            Assert.AreEqual(0, result.HandlerDelete);
            Assert.AreEqual(0, result.HandlerDiscover);
            Assert.AreEqual(0, result.HandlerPrepare);
            Assert.AreEqual(1, result.HandlerReadFirst);
            Assert.AreEqual(2, result.HandlerReadKey);
            Assert.AreEqual(0, result.HandlerReadLast);
            Assert.AreEqual(0, result.HandlerReadNext);
            Assert.AreEqual(0, result.HandlerReadPrev);
            Assert.AreEqual(0, result.HandlerReadRnd);
            Assert.AreEqual(3, result.HandlerReadRndNext);
            Assert.AreEqual(0, result.HandlerRollback);
            Assert.AreEqual(0, result.HandlerSavepoint);
            Assert.AreEqual(0, result.HandlerSavepointRollback);
            Assert.AreEqual(0, result.HandlerUpdate);
            Assert.AreEqual(0, result.HandlerWrite);
        }
    }

}
